<?php
/**
 * model 模型类 
 * 调用数据库工厂类提供的数据库类方法实现应用层操作
 * @author yangzhiguo <yangzhiguo0903@163.com>
 */
defined('IN_LK') or die('Access Denied');
abstract class model
{
	/**
	 * 数据库连接标志
	 */
	protected static $db;
	
	/**
	 * 数据表前缀
	 */
	protected $db_pre;
	
	/**
	 * 数据表名 无需加前缀
	 */
	protected $tablename;

	public function __construct()
	{
		lk::lo('db_factory', '', 0);
		$this->db_pre = DB_PRE;
		$this->tablename = $this->db_pre . $this->tablename;
		self::$db = db_factory::initialize();
	}
	
	/**
	 * 执行sql 替换表前缀
	 * @param string $sql 需要执行的sql
	 * @param string $type UNBUFFERED | SLIENT 不获取结果 静默执行
	 */
	final public function query($sql, $type = '')
	{
		$sql = str_replace(DB_PRE, $this->db_pre, $sql);
		return self::$db->query($sql);
	}
	
	/**
	 * 查询
	 * @param string $field 字段    'id,name'
	 * @param string $where 条件    '`id`=1 AND ...'	需要加`
	 * @param string $order 排序
	 * @param string $group 分组
	 * @param string $limit 查询条数
	 * @param string $key   关键字段
	 * @return array        查询结果集的数组
	 */
	final public function get($field, $where = '', $order = '', $group = '', $limit = '', $key = '')
	{
		return self::$db->get($field, $this->tablename, $where, $order, $group, $limit, $key);
	}
	
	/**
     * 查询单条记录
	 * @param string $field 字段    'id,name'
	 * @param string $where 条件    '`id`=1 AND ...'	需要加`
	 * @param string $order 排序
	 * @param string $group 分组
	 * @return array        查询结果集的数组
	*/
	final public function get_one($field, $where = '', $order = '', $group = '')
	{
		return self::$db->get_one($field, $this->tablename, $where, $order, $group);
	}

	/**
	 * 查询 整合优化分页 分界点5000
	 * @param string $field 查询字段
	 * @param string $where 查询条件
	 */
	final public function listinfo($field = '*', $where = '', $page = 1, $pagesize = 25, $order = '', $group = '', $key = '', $url = '')
	{
		$this->page_nav = '';
		$this->num = $this->_count($where);
		if($this->num <=0)
		{
			return false;
		}
		$page = max((int)$page, 1);
		$this->page_nav = page($this->num, $page, $pagesize, 1, 0, 1, 0, $url);
		$offset = ($page - 1) * $pagesize;
		if($offset <5000)
		{
			return $this->get($field, $where, $order, $group, "$offset, $pagesize", $key);
		}
		else
		{
			$primary = $this->get_primary();
			$pagewhere = "`$primary` > (SELECT `$primary` FROM `$this->tablename` LIMIT $offset, 1)";
			$where = $where ? "$pagewhere AND $where" : $pagewhere;
			return $this->get($field, $where, $order, $group, $pagesize, $key);
		}
	}

	/**
	 * 插入数据 返回上次插入的主键id
	 * @param array    $data      键值对数组
	 * @param boolean  $replace   是否替换
	 * @return int 主键id
	 */
	final public function insert($data, $replace = false)
	{
		return self::$db->insert($data, $this->tablename, true, $replace);
	}
	
	/**
	 * 查询数据条数
	 */
	final public function _count($where = '')
	{
		$res = $this->get("COUNT(*) AS `num`", $where);
		return $res[0]['num'];
	}
	
    /**
     * 更新数据 返回影响的行数
	 * @param array    $data      键值对数组 array('name'=>'abc','logintimes'=>'+=1','lost'=>'-=1')
	 * @param string   $where     条件
	 * @param boolean/int         返回影响的行数或是执行成功
	 */
	final public function update($data, $where)
	{
		return self::$db->update($data, $this->tablename, $where, true);
	}
	
    /**
     * 删除数据
	 * @param strign  $where     条件
	 * @param boolean 返回是否成功
	 */
	final public function delete($where)
	{
		return self::$db->delete($this->tablename, $where, true);
	}

    /**
     * 获得数据表主键
	 * @return string 主键字段名
	 */
	final public function get_primary()
	{
		return self::$db->get_primary($this->tablename);
	}
	
	/**
	 * 数据查询条数
	 */
	final public static function querynum()
	{
		return self::$db->querynum;
	}
}